﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SP2013EventManager {
    public class Helpers {

        public static List<SPWebApplication> webAppsList = new List<SPWebApplication>();

        /// <summary>
        /// Get all web applications
        /// </summary>
        /// <param name="isRefresh">if true - refresh immediately (get web apps), if false - first check if list has items</param>
        /// <returns></returns>
        public static List<SPWebApplication> GetAllWebApplications(bool isRefresh) {

            if (!isRefresh) {
                if (webAppsList.Count == 0)
                    GetAllWebApplications(true);
            } else {
                SPServiceCollection services = SPFarm.Local.Services;
                foreach (SPService curService in services) {
                    if (curService is SPWebService) {
                        SPWebService webService = (SPWebService)curService;
                        foreach (SPWebApplication webApp in webService.WebApplications) {
                            webAppsList.Add(webApp);
                        }
                    }
                }
            }
            return webAppsList;
        }


        /// <summary>
        /// Fill ComboBox with items of type SPWebApplication
        /// </summary>
        /// <param name="comboBox"></param>
        public static void FillWebAppComboBox(ComboBox comboBox) {
            comboBox.Items.Clear();
            foreach (SPWebApplication webApp in Helpers.webAppsList) {
                comboBox.Items.Add(webApp);
            }
        }


        public static void RefreshListBox(SPList list, ListBox listBox) {
            foreach (SPEventReceiverDefinition item in list.EventReceivers) {
                listBox.Items.Add(item);
            }
        }


        public static void EnsureEventReceiver(
                                    SPList list, 
                                    IEnumerable<SPEventReceiverType> receiverTypes, 
                                    string eventAssembly, 
                                    string eventClass, 
                                    SPEventReceiverSynchronization synchronization, 
                                    int sequenceNumber) {

                                        try {
                                            foreach (SPEventReceiverType spEventReceiverType in receiverTypes) {
                                                string name = list.Title + spEventReceiverType.ToString();
                                                if (list.EventReceivers.Cast<SPEventReceiverDefinition>().All(i => i.Name != name)) {
                                                    SPEventReceiverDefinition eventReceiver = list.EventReceivers.Add();
                                                    eventReceiver.Name = name;
                                                    eventReceiver.Type = spEventReceiverType;
                                                    eventReceiver.Assembly = eventAssembly;
                                                    eventReceiver.Class = eventClass;
                                                    eventReceiver.SequenceNumber = sequenceNumber;
                                                    eventReceiver.Synchronization = synchronization;
                                                    eventReceiver.Update();
                                                }
                                            }
                                        } catch (Exception e) {
                                            MessageBox.Show("ERROR: " + e.Message);
                                        }
            

        }
    }
}
